/**
 * Create By FengZheng
 * On 2019/11/6 4:54
 */
(function ($) {
    "use strict";

    let $dom = {
        $summerNote: $("#edit-area"),
        $submit: $("#sub-btn"),
        $role: $("#role"),
        $order: $("#order"),
        $isEnable: $("#enable"),
        $title: $("#title"),
        $errorBox: $("#error-box"),
        $errorContent: $("#error-content"),
        $alertBox: $("#alert-box"),
        $confirmBtn: $("#confirm"),
        $cancelBtn: $("#cancel")
    };

    let $system = {
        data: null
    };

    /**
     * 点击了提交
     */
    $dom.$submit.on("click",function () {
        // 禁用按钮
        $(this).attr("disabled",true);

        let id = $(this).attr("data-id");
        let role = $dom.$role.val();
        let order = $dom.$order.val().trim();
        let isEnable = $dom.$isEnable.val();
        let title = $dom.$title.val().trim();
        let content = $dom.$summerNote.summernote("code").trim();

        // ID验证
        if(id === "" || id === 0){
            return openError("无法获取到关键参数，请重新刷新此页面后尝试");
        }

        // 文档标题验证
        if(title.length < 1 || title.length > 100){
            return openError("文档标题不正确");
        }

        // 排序验证
        if(order === ""){
            order = 1;
        }else {
            order = parseInt(order);
            if(!(/^\d{1,5}$/.test(order))){
                return openError("排序序号不正确");
            }
        }

        // 输入内容验证
        if(content === ""){
            return openError("未检测到有任何输入");
        }

        $system.data = {
            "id": id,
            "role": role,
            "order": order,
            "enable": isEnable,
            "title": title,
            "content": content
        };

        // 未启用时弹出警告框
        if(isEnable === "0"){
            return $dom.$alertBox.modal("show");
        }

        doRequest();
    });

    /**
     * 确认提交
     */
    $dom.$confirmBtn.on("click",function () {
        doRequest();
    });

    /**
     * 提交
     */
    function doRequest(){
        if($system.data === null){
            return openError("请先填写要提交的数据");
        }

        $.ajax({
            url: "/admin/frame/update-doc-sub",
            type: "post",
            dataType: "json",
            data: $system.data,
            success: function (resultD) {
                if( (typeof resultD)==="object" ){
                    let status = resultD.status;
                    if(status === 200){
                        return window.top.changeIframe("/admin/frame/documentManager");
                    }else{
                        return openError(resultD.info);
                    }
                }
                openError("服务器返回异常信息，请稍后重试！");
            },
            error: function (jqXHR) {
                openError(jqXHR.status);
            }
        });
    }

    /**
     * 点击了返回修改
     */
    $dom.$cancelBtn.on("click",function () {
        $dom.$submit.attr("disabled",false);
    });

    /**
     * 打开错误提示框
     */
    function openError(msg) {
        $dom.$errorBox.modal("show");
        $dom.$errorContent.text(msg);

        $dom.$submit.attr("disabled",false);
    }

    /**
     * 初始化编辑器
     */
    function initEdit() {
        $dom.$summerNote.summernote({
            height: 450,
            focus: true,
            lang: 'zh-CN',
            toolbar: [
                ['edit',['undo','redo']],
                ['headline', ['style']],//大字标题
                ['style', ['bold', 'italic', 'underline', 'superscript', 'subscript', 'strikethrough', 'clear']],//样式
                ['fontface', ['fontname','color','fontsize']],//字体
                ['alignment', ['ul', 'ol', 'paragraph', 'lineheight']],//对齐方式
                ['height', ['height']],//行高(自定义行高时一定不要忘记写这个)
                ['table', ['table']],//表单
                ['insert', ['link','picture','video','hr']],//插入链接，图片，下划线
                ['view', ['fullscreen', 'codeview']]//全屏，代码视图
            ]
        });
    }

    $(document).ready(function () {
        initEdit();
    });
}(jQuery));